package board.animation.tranlations;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.image.ImageObserver;

import board.PointSpaceTranslator;
import board.grid.Space;

public class LinearTranslation<T> implements Translation<T> {
	private Space start;
	private int deltaX, deltaY;
	private double offset_x, offset_y;
	private int speed;
	private int step;
	private T occupant;
	
	@Override
	public void draw(Image render, Graphics2D area, ImageObserver i, PointSpaceTranslator t) {
		Point p = t.spaceToPoint_withOffsets(start, offset_x, offset_y);
		area.drawImage(render, p.x - (render.getWidth(i) - t.getCurrentCellSize()) / 2, p.y - (render.getHeight(i) - t.getCurrentCellSize()) / 2, i);
	}
	@Override
	public void setOccupant(T occupant) {this.occupant = occupant;}
	@Override
	public T getOccupant() {return occupant;}
	@Override
	public boolean isComplete() {
		boolean r = true;
		if(deltaX > 0)
			r &= (int)offset_x >= deltaX;
		else if (deltaX < 0)
			r &= (int)offset_x <= deltaX;
		if(deltaY > 0)
			r &= (int)offset_y >= deltaY;
		else if (deltaY < 0)
			r &= (int)offset_y <= deltaY;
		return r;
	}
	@Override
	public void next() {
		if(!isComplete()) {
			step++;
			offset_x = (step * deltaX) / (double)speed;
			offset_y = (step * deltaY) / (double)speed;
		}
	}
	@Override
	public void setTranslation(Space start, Space end, int speed) {
		this.start = start;
		deltaX = end.getColumn() - start.getColumn();
		deltaY = end.getRow() - start.getRow();
		this.speed = speed;
		offset_x = offset_y = 0.0;
		step = 0;
	}	
}